package com.example.meituan.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.meituan.R;
import com.example.meituan.adapter.CustomAdapter;
import com.example.meituan.bean.OrderBean;
import com.example.meituan.bean.ShopBean;
import com.example.meituan.utils.Constant;
import com.example.meituan.utils.JsonParse;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class OrderHistoryActivity extends AppCompatActivity {
    private ListView listView;
    private CustomAdapter adapter;
    private ArrayList<String> namesList = new ArrayList<>();
    private String jsonData;
    private MHandler handler;
    public static final int MSG_AD_SLID = 1;  //广告自动滑动
    public static final int MSG_SHOP_OK = 2;  //获取数据
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_history);
        getSupportActionBar().hide();//隐藏标题栏
        listView = findViewById(R.id.listView);
        handler = new MHandler();
        // 假设你有一个方法来获取 JSON 数据并解析它
        // 这里简单示范一个模拟的数据解析过程
        parseJSONData();
        Button button = findViewById(R.id.goshopping);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(OrderHistoryActivity.this, ShopActivity.class);
                startActivity(intent);
                finish();

            }
        });
        BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation);
        bottomNavigationView.getMenu().findItem(R.id.navigation_home).setChecked(false);
        bottomNavigationView.getMenu().findItem(R.id.navigation_profile).setChecked(true);
        // 设置监听器以响应底部导航栏中项目的选定事件
        bottomNavigationView.setOnNavigationItemSelectedListener(item -> {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    // 处理菜单项1的点击事件
                    // 可以在这里执行相应的操作
                    System.out.println("跳转到店铺");
                    Intent tintent = new Intent(OrderHistoryActivity.this, ShopActivity.class);
                    startActivity(tintent);
                    finish();
                    overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
                    return true;
                case R.id.navigation_profile:

                    return true;
                // 可以添加更多菜单项的处理
                // ...

                default:
                    return false;
            }
        });
    }

    private void parseJSONData() {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder().url(Constant.WEB_SITE +
                Constant.REQUEST_ORDER_URL).build();
        Call call = okHttpClient.newCall(request);

        // 假设这是你从服务端获取的 JSON 数据
        //String jsonData = "[{\"name\":\"Name 1\"},{\"name\":\"Name 2\"},{\"name\":\"Name 3\"}]";
        call.enqueue(new Callback() {
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                jsonData = response.body().string(); //获取店铺数据
                Message message = new Message();
                message.what = 1;
                message.obj = jsonData;
                handler.sendMessage(message);
                //List<OrderBean> orderBeanList = JsonParse.getInstance().getOrderList(jsonData);

//                CustomAdapter customAdapter = new CustomAdapter(OrderHistoryActivity.this,orderBeanList);
//                ListView lv = (ListView)findViewById(R.id.listView);
//                lv.setAdapter(customAdapter);
            }

            @Override
            public void onFailure(Call call, IOException e) {
            }
        });

    }
    class MHandler extends Handler {
        //处理子线程发送过来的消息
        @Override
        public void dispatchMessage(Message msg) {
            super.dispatchMessage(msg);
            switch (msg.what) {
                case 1:
                    // 更新UI操作
                    List<OrderBean> orderBeanList = JsonParse.getInstance().getOrderList((String)msg.obj);
                    for(int i = 0;i<orderBeanList.size();i++){
                        System.out.println(orderBeanList.get(i).getShopName());
                    }
                    adapter = new CustomAdapter(OrderHistoryActivity.this, orderBeanList);
                    listView.setAdapter(adapter);
                    break;
            }
        }
    }
}
